<?php
namespace App\Admin\Controllers\OverSeasIn;

use App\Admin\Actions\OverSeasIn\ImportOSMarksExcel;
use App\Models\OverSeasIn\MarksPDF;
use Encore\Admin\Admin as RAdmin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
class OSMarksCreateController extends AdminController{

    protected $title = '箱唛生成工具';

    protected function grid()
    {
        RAdmin::script($this->_script());
        $grid = new Grid(new MarksPDF());
        $grid->model()->orderBy('id', 'DESC');
        $grid->column('delivery_no','发货单');
        $grid->column('template','箱唛模板')->using(MarksPDF::$_template);
        $grid->column('created_at','创建时间');
        $grid->column('操作')->view('overseas-in.os-marks-create.action');
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportOSMarksExcel());
        });

        $grid->disableActions();
        $grid->fixHeader();
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->equal('delivery_no','发货单');
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('template', '箱唛模板')->select(MarksPDF::$_template);
            });
        });
        return $grid;
    }

    protected function _script()
    {
        $token = csrf_token();
        return <<<EOT
$('.grid-row-delete').unbind('click').click(function() {
    var id = $(this).data('id');
    swal({
        title: "确认删除?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "确认",
        showLoaderOnConfirm: true,
        cancelButtonText: "取消",
        preConfirm: function() {
            return new Promise(function(resolve) {
                $.ajax({
                    method: 'post',
                    url: '/xqc/overseas-in/os-marks-create/' + id,
                    data: {
                        _method:'delete',
                        _token: "{$token}",
                    },
                    success: function (data) {
                        resolve(data);
                    }
                });
            });
        }
    }).then(function(result) {
        var data = result.value;
        if (typeof data === 'object') {
            if (data.status) {
                $.pjax.reload('#pjax-container');
                toastr.success('删除成功 !');
            } else {
                swal(data.message, '', 'error');
            }
        }
    });
});
EOT;
    }

    protected function download($id){
        $re = MarksPDF::query()->findOrFail($id);
        if(!$re) {
            admin_toastr('无文件记录！','error');
            return back();
        }
        try{
            $file = MarksPDF::storePath($re->delivery_no,$re->template);
            if(!file_exists($file)){
                admin_toastr('无文件！','error');
                return back();
            }
            return response()->download($file);
        }catch (\Exception $e){
            report($e);
            admin_toastr('操作失败！','error');
            return back();
        }
    }

    public function form()
    {
        return new Form(new MarksPDF());
    }
}
